package com.chuanyang.driverh.mainhome;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;

import java.util.ArrayList;
import java.util.List;



public class FilterActivity extends BaseTitleActivity {
    private String type;//哪个页面的筛选
    private RequestFilter filter;

    private RadioGroup rg_common;
    //    private int filterType;//0,起止地点，1，起止时间，2，状态，3，类型。
    private TextView tv_title;
    private LinearLayout ll_addr, ll_time, ll_status, ll_status2,ll_type;
    private TextView tv_addr_start, tv_addr_end;
    private LinearLayout ll_today, ll_month, ll_custom;
    private ImageView iv_today, iv_month, iv_custom;
    private TextView tv_time_start, tv_time_end;

//    private MyRadioGroup rg_status;
//    private MyRadioGroup rg_type;
    private CheckBox cb0, cb1, cb2, cb3;
    private CheckBox rb_biding, rb_match, rb_special;

    private TextView tv_filter_clear, tv_filter;//按钮

    private List<CheckBox> rbOrderStatus;
    private List<CheckBox> rbOrdertype;

    @Override
    protected int getContentResId() {
        return R.layout.home_act_filter;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rg_common = (RadioGroup) findViewById(R.id.rg_common);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ll_addr = (LinearLayout) findViewById(R.id.ll_addr);
        ll_time = (LinearLayout) findViewById(R.id.ll_time);
        ll_status = (LinearLayout) findViewById(R.id.ll_status);
        ll_status2 = (LinearLayout) findViewById(R.id.ll_status2);
        ll_type = (LinearLayout) findViewById(R.id.ll_type);
        tv_addr_start = (TextView) findViewById(R.id.tv_addr_start);
        tv_addr_end = (TextView) findViewById(R.id.tv_addr_end);

        ll_today = (LinearLayout) findViewById(R.id.ll_today);
        ll_month = (LinearLayout) findViewById(R.id.ll_month);
        ll_custom = (LinearLayout) findViewById(R.id.ll_custom);
        iv_today = (ImageView) findViewById(R.id.iv_today);
        iv_month = (ImageView) findViewById(R.id.iv_month);
        iv_custom = (ImageView) findViewById(R.id.iv_custom);
        tv_time_start = (TextView) findViewById(R.id.tv_time_start);
        tv_time_end = (TextView) findViewById(R.id.tv_time_end);
//        rg_status = (MyRadioGroup) findViewById(R.id.rg_status);
//        rg_type = (MyRadioGroup) findViewById(R.id.rg_type);

        cb0 = (CheckBox) findViewById(R.id.cb0);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        rb_biding = (CheckBox) findViewById(R.id.rb_biding);
        rb_match = (CheckBox) findViewById(R.id.rb_match);
        rb_special = (CheckBox) findViewById(R.id.rb_special);


        tv_filter_clear = (TextView) findViewById(R.id.tv_filter_clear);
        tv_filter = (TextView) findViewById(R.id.tv_filter);
    }

    @Override
    protected void initData() {

        rbOrderStatus=new ArrayList<CheckBox>();
        rbOrdertype=new ArrayList<CheckBox>();
        rbOrderStatus.add(cb0);
        rbOrderStatus.add(cb1);
        rbOrderStatus.add(cb2);
        rbOrderStatus.add(cb3);

        rbOrdertype.add(rb_biding);
        rbOrdertype.add(rb_match);
        rbOrdertype.add(rb_special);

        type = getIntent().getStringExtra("type");
        filter = (RequestFilter) (getIntent().getSerializableExtra("data"));
        if (filter == null) {
            filter = new RequestFilter();
        }
        setBackImg();
        if ("1".equals(type)){//1接单，2，车辆安排，3代运实发，4收款跟踪
            getTitleTextView().setText("接单(筛选)");
            ll_status2.setVisibility(View.GONE);
            cb0.setText("已接单");
            cb1.setText("待处理");
            cb2.setVisibility(View.GONE);
        }else if("2".equals(type)){
            getTitleTextView().setText("车辆安排(筛选)");
            ll_status2.setVisibility(View.GONE);
            cb0.setText("待派单");
            cb1.setText("已派单");
            cb2.setText("派单中");
            cb2.setVisibility(View.VISIBLE);

        }else if("3".equals(type)){
            getTitleTextView().setText("代运实发(筛选)");
            ll_status2.setVisibility(View.VISIBLE);
            cb0.setText("待装货");
            cb1.setText("运输中");
            cb2.setText("待收货");
            cb3.setText("已完成");
            cb2.setVisibility(View.VISIBLE);
            cb3.setVisibility(View.VISIBLE);

        }else if("4".equals(type)){
            getTitleTextView().setText("收款跟踪(筛选)");
            ll_status2.setVisibility(View.GONE);
            cb0.setText("已收款");
            cb1.setText("未收款");
            cb2.setVisibility(View.GONE);
        }

        setUiVisible(0);
        setUiData();
    }


    @Override
    protected void initListener() {
        tv_filter_clear.setOnClickListener(this);
        tv_filter.setOnClickListener(this);

        ll_today.setOnClickListener(this);
        ll_month.setOnClickListener(this);
        ll_custom.setOnClickListener(this);

        tv_addr_start.setOnClickListener(this);
        tv_addr_end.setOnClickListener(this);
        tv_time_start.setOnClickListener(this);
        tv_time_end.setOnClickListener(this);
        rg_common.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb0:
                        setUiVisible(0);
                        tv_title.setText("起止");
                        break;
                    case R.id.rb1:
                        setUiVisible(1);
                        tv_title.setText("时间");
                        break;
                    case R.id.rb2:
                        setUiVisible(2);
                        tv_title.setText("状态");
                        break;
                    case R.id.rb3:
                        setUiVisible(3);
                        tv_title.setText("类型");
                        break;
                }
            }
        });
    }

    private void setUiVisible(int position) {
        ll_addr.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
        ll_time.setVisibility(position == 1 ? View.VISIBLE : View.GONE);
        ll_status.setVisibility(position == 2 ? View.VISIBLE : View.GONE);
        ll_type.setVisibility(position == 3 ? View.VISIBLE : View.GONE);
    }

    private void setUiData() {
        if (filter != null) {
            if (!StringUtil.isBlank(filter.getStartPoint())) {
                tv_addr_start.setText(filter.getStartPoint());
            } else {
                tv_addr_start.setText("");
            }
            if (!StringUtil.isBlank(filter.getEndPoint())) {
                tv_addr_end.setText(filter.getEndPoint());
            } else {
                tv_addr_end.setText("");
            }
            if (!StringUtil.isBlank(filter.getSelectType())) {
                if ("2".equals(filter.getSelectType())) {//1当日，2当月，3自定义
                    setTime(1);
                } else if ("3".equals(filter.getSelectType())) {
                    setTime(2);
                } else if ("0".equals(filter.getSelectType())) {
                    setTime(0);
                }else {
                    setTime(3);
                }
            }else{
                setTime(3);
                filter.setSelectType(null);
            }

            if (!StringUtil.isBlank(filter.getStartTime())) {
                tv_time_start.setText(filter.getStartTime());
            }else {
                tv_time_start.setText("");
            }
            if (!StringUtil.isBlank(filter.getEndTime())) {
                tv_time_end.setText(filter.getEndTime());
            }else{
                tv_time_end.setText("");
            }


            for (CheckBox rb:rbOrdertype){
                rb.setChecked(false);
            }
            for (CheckBox rb:rbOrderStatus){
                rb.setChecked(false);
            }
            if (filter.getOrderStatus2()!=null && filter.getOrderStatus2().size()>0){
                for (int i=0;i<filter.getOrderStatus2().size();i++){
                    int status = filter.getOrderStatus2().get(i);
                    rbOrderStatus.get(status-1).setChecked(true);
                }
            }
            if (filter.getOrderType2()!=null && filter.getOrderType2().size()>0){
                for (int i=0;i<filter.getOrderType2().size();i++){
                    int type = filter.getOrderType2().get(i);
                    rbOrdertype.get(type-1).setChecked(true);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_filter_clear:
                clearFilter();
                break;
            case R.id.tv_filter:
                filter();
                break;
            case R.id.ll_today:
                filter.setSelectType("1");
                setTime(0);
                break;
            case R.id.ll_month:
                filter.setSelectType("2");
                setTime(1);
                break;
            case R.id.ll_custom:
                filter.setSelectType("3");
                setTime(2);
                break;
            case R.id.tv_addr_start:
                DialogUtil.cityPick(activityContext, tv_addr_start);
                break;
            case R.id.tv_addr_end:
                DialogUtil.cityPick(activityContext, tv_addr_end);
                break;
            case R.id.tv_time_start:
                DialogUtil.onYearMonthDayTimePicker(activityContext, tv_time_start);
                break;
            case R.id.tv_time_end:
                DialogUtil.onYearMonthDayTimePicker(activityContext, tv_time_end);
                break;
        }
    }

    private void setTime(int timeType) {
        iv_today.setVisibility(timeType == 0 ? View.VISIBLE : View.INVISIBLE);
        iv_month.setVisibility(timeType == 1 ? View.VISIBLE : View.INVISIBLE);
        iv_custom.setVisibility(timeType == 2 ? View.VISIBLE : View.INVISIBLE);


        tv_time_start.setEnabled(timeType == 2);
        tv_time_end.setEnabled(timeType == 2);
        tv_time_start.setBackgroundResource(timeType == 2 ? R.drawable.et_shape_round_corner_geen : R.drawable.et_shape_round_corner_gray);
        tv_time_end.setBackgroundResource(timeType == 2 ? R.drawable.et_shape_round_corner_geen : R.drawable.et_shape_round_corner_gray);
    }


    private void clearFilter() {
        filter = new RequestFilter();
        filter.setSelectType(null);
        filter.setOrderStatus2(null);
        filter.setOrderType2(null);
        setUiData();
    }

    private void filter() {
        filter.setStartPoint(tv_addr_start.getText().toString().trim());
        filter.setEndPoint(tv_addr_end.getText().toString().trim());
        if ("3".equals(filter.getSelectType())) {
            if (StringUtil.isBlank(tv_time_start.getText().toString().trim())) {
                PrintUtil.toast(activityContext, "请选择装货时间");
                return;
            }
            if (StringUtil.isBlank(tv_time_end.getText().toString().trim())) {
                PrintUtil.toast(activityContext, "请选择卸货时间");
                return;
            }
            filter.setStartTime(tv_time_start.getText().toString().trim());
            filter.setEndTime(tv_time_end.getText().toString().trim());
        }else{
            filter.setStartTime(null);
            filter.setEndTime(null);
        }

           List<Integer> requestStatus=new ArrayList<>();
           List<Integer> requestType=new ArrayList<>();

        for (int i=0;i<rbOrderStatus.size();i++){
            if (rbOrderStatus.get(i).isChecked())requestStatus.add(i+1);
        }
        for (int i=0;i<rbOrdertype.size();i++){
            if (rbOrdertype.get(i).isChecked())requestType.add(i+1);
        }

        filter.setOrderStatus2(requestStatus);
        filter.setOrderType2(requestType);


        EventBusUtil.postEvent(new FilterEvent(type, filter));
        finish();

    }
}
